using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;

namespace CKY.AgentPlatform.Caching
{
    /// <summary>
    /// 缓存服务接口
    /// </summary>
    public interface ICacheService : IApplicationService
    {
        Task<T> GetAsync<T>(string key, Func<Task<T>> factory, TimeSpan? expiration = null);
        Task SetAsync<T>(string key, T value, TimeSpan? expiration = null);
        Task<T> GetAsync<T>(string key);
        Task<bool> ExistsAsync(string key);
        Task RemoveAsync(string key);
        Task RemoveByPrefixAsync(string prefix);
        Task ClearAsync();
        Task<Dictionary<string, T>> GetManyAsync<T>(IEnumerable<string> keys);
        Task SetManyAsync<T>(IDictionary<string, T> items, TimeSpan? expiration = null);
        Task RemoveManyAsync(IEnumerable<string> keys);
        Task<InternalCacheStatisticsDto> GetStatisticsAsync();
    }

    /// <summary>
    /// 分布式缓存服务接口
    /// </summary>
    public interface IDistributedCacheService : ICacheService
    {
        Task<T> GetLocalAsync<T>(string key);
        Task SetLocalAsync<T>(string key, T value, TimeSpan? expiration = null);
        Task<bool> RefreshAsync(string key);
        Task<Dictionary<string, object>> GetSystemInfoAsync();
        Task<CacheHealthDto> GetHealthAsync();
    }

    /// <summary>
    /// Redis缓存服务接口
    /// </summary>
    public interface IRedisCacheService : IDistributedCacheService
    {
        Task<bool> PingAsync();
        Task<long> GetDatabaseSizeAsync();
        Task<Dictionary<string, string>> GetInfoAsync();
        Task<List<string>> GetKeysAsync(string pattern = "*");
        Task<bool> KeyExistsAsync(string key);
        Task<TimeSpan?> GetKeyExpirationAsync(string key);
        Task SetKeyExpirationAsync(string key, TimeSpan expiration);
        Task<IDictionary<string, T>> HashGetAllAsync<T>(string key);
        Task<bool> HashSetAsync<T>(string key, string field, T value);
        Task<T> HashGetAsync<T>(string key, string field);
        Task<bool> HashDeleteAsync(string key, string field);
        Task<long> HashLengthAsync(string key);
        Task<RedisStatisticsDto> GetRedisStatisticsAsync();
    }

    /// <summary>
    /// 缓存健康检查服务
    /// </summary>
    public interface ICacheHealthCheck
    {
        Task<CacheHealthDto> CheckHealthAsync();
        Task<List<CacheDiagnosticDto>> RunDiagnosticsAsync();
        Task<bool> TestConnectionAsync();
        Task<Dictionary<string, object>> GetPerformanceMetricsAsync();
    }

    /// <summary>
    /// 内部缓存统计数据
    /// </summary>
    public class InternalCacheStatisticsDto
    {
        public long TotalKeys { get; set; }
        public long TotalRequests { get; set; }
        public long CacheHits { get; set; }
        public long CacheMisses { get; set; }
        public double HitRate { get; set; }
        public long TotalSizeBytes { get; set; }
        public Dictionary<string, long> KeyTypeDistribution { get; set; }
        public Dictionary<string, double> AverageResponseTimes { get; set; }
        public DateTime LastUpdated { get; set; }
    }

    /// <summary>
    /// 缓存健康状态
    /// </summary>
    public class CacheHealthDto
    {
        public bool IsHealthy { get; set; }
        public string Status { get; set; }
        public DateTime CheckTime { get; set; }
        public double ResponseTimeMs { get; set; }
        public string Message { get; set; }
        public Dictionary<string, object> Details { get; set; }
        public List<string> Warnings { get; set; }
        public List<string> Errors { get; set; }
    }

    /// <summary>
    /// 缓存诊断信息
    /// </summary>
    public class CacheDiagnosticDto
    {
        public string TestName { get; set; }
        public string Description { get; set; }
        public bool Passed { get; set; }
        public string Message { get; set; }
        public double ExecutionTimeMs { get; set; }
        public Dictionary<string, object> Details { get; set; }
        public string Recommendation { get; set; }
    }

    /// <summary>
    /// Redis统计数据
    /// </summary>
    public class RedisStatisticsDto
    {
        public bool IsConnected { get; set; }
        public string Version { get; set; }
        public long DatabaseSize { get; set; }
        public long UsedMemory { get; set; }
        public long MaxMemory { get; set; }
        public double MemoryUsagePercentage { get; set; }
        public int ConnectedClients { get; set; }
        public long TotalCommandsProcessed { get; set; }
        public long KeyspaceHits { get; set; }
        public long KeyspaceMisses { get; set; }
        public double HitRate { get; set; }
        public DateTime Uptime { get; set; }
        public Dictionary<string, string> Info { get; set; }
        public Dictionary<string, object> PerformanceMetrics { get; set; }
    }
}